<?php

// Help us to pick out errors, if any.
ini_set('error_reporting', E_ALL & ~E_NOTICE);
ini_set('display_errors', true);
ini_set('memory_limit', '64M');
define('MAGPIE_DEBUG', true);

// Are we running from a web request or from the command line?
		$update_feeds_display = 'text/plain';
		$update_feeds_invoke = 'post';
		$update_feeds_verbose = false;

require_once ('../wp-blog-header.php');

function update_feeds_mention ($feed) {
	global $update_feeds_display;
	
	if ($update_feeds_display=='text/html') :
		echo "<li>Updating <cite>".$feed['link/name']."</cite> from &lt;<a href=\""
			.$feed['link/uri']."\">".$feed['link/uri']."</a>&gt; ...</li>\n";
	else :
		echo "* Updating ".$feed['link/name']." from <".$feed['link/uri']."> ...\n";
	endif;
	flush();
}

# -- Don't change these unless you know what you're doing...
define ('RPC_MAGIC', 'tag:radgeek.com/projects/feedwordpress/'); // update all

// Query secret word from database
$rpc_secret = get_settings('feedwordpress_rpc_secret');

header("Content-Type: {$update_feeds_display}; charset=utf-8");


	// Henceforward, we can proceed on the assumption that we have an authenticated user
	$uri = (isset($_REQUEST['uri']) ? $_REQUEST['uri'] : RPC_MAGIC.$rpc_secret);

	if ($update_feeds_display=='text/html') :
		echo <<<EOHTML
<?xml version="1.0" encoding="utf-8"?>
<html>
<head>
<title>update-feeds :: FeedWordPress</title>
</head>

<body>
<h1>update-feeds: make FeedWordPress check for new syndicated content</h1>

EOHTML;
	endif;

$feedwordpress =& new FeedWordPress;

if ($update_feeds_display=='text/html' or $update_feeds_verbose) :
	add_action('feedwordpress_check_feed', 'update_feeds_mention');
endif;

if ($update_feeds_display=='text/html') : // HTTP GET or HTTP POST: add some web niceties

	echo "<form action=\"\" method=\"POST\">\n";
	echo "<select name=\"uri\">\n";
	echo "<option value=\"".RPC_MAGIC.$rpc_secret."\">All feeds</option>\n";
	foreach ($feedwordpress->feeds as $feed) :
		echo "<option value=\"{$feed['link/uri']}\"";
		if ($feed['link/uri']==$_REQUEST['uri']) : echo ' selected="selected"'; endif;
		echo ">{$feed['link/name']}</option>\n";
	endforeach;
	echo "</select> ";
	echo "<input type=\"submit\" name=\"update\" value=\"Update\" />\n";
	echo "</form>\n";
endif;

if ($update_feeds_invoke != 'get') : // Only do things with side-effects for HTTP POST or command line
	if ($update_feeds_display == 'text/html') : echo "<ul>\n"; endif;
	$delta = @$feedwordpress->update($uri);
	if ($update_feeds_display == 'text/html') : echo "</ul>\n"; endif;

	if (is_null($delta)) :
		if ($update_feeds_invoke == 'cmd') :
			$stderr = fopen('php://stderr', 'w');
			fputs($stderr, "update-feeds (".date('Y-m-d H:i:s')."): ERROR: I don't syndicate <$uri>\n");
		elseif ($update_feeds_display == 'text/plain') :
			echo "update-feeds (".date('Y-m-d H:i:s')."): ERROR: I don't syndicate <$uri>\n";
		else :
			echo "<p><strong>Error:</strong> I don't syndicate <a href=\"$uri\">$uri</a></p>\n";
		endif;
	elseif ($update_feeds_display=='text/html' or $update_feeds_verbose) :
		$mesg = array();
		if (isset($delta['new'])) : $mesg[] = ' '.$delta['new'].' new posts were syndicated'; endif;
		if (isset($delta['updated'])) : $mesg[] = ' '.$delta['updated'].' existing posts were updated'; endif;
		if ($update_feeds_display=='text/html') : echo "<p>"; endif;
		echo "Update complete.".implode(' and', $mesg);
		if ($update_feeds_display=='text/html') : echo "</p>"; endif;
		echo "\n"; flush();
	endif;
endif;

if ($update_feeds_display=='text/html') : // HTTP GET or HTTP POST: close off web niceties
	echo <<<EOHTML

<p><a href="../wp-admin">&larr; Return to WordPress Dashboard</a></p>
</body>
</html>
EOHTML;
endif;
?>
